/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.marist.cs.os.g3.processors;

import edu.marist.cs.os.g3.mos.Buffer;
import java.io.PrintWriter;

/**
 * This channel takes an output-full buffer and returns the buffer empty.
 * @author Cameron
 */
public class Channel2 {    
    public static final int TIME_REQUIRED = 5;
    public static final int IOI_VALUE = 2;
    int timer = 0;
    Buffer buffer = new Buffer();
    boolean busy = false;
    CPU cpu;

    PrintWriter output;
    
    public Channel2(CPU cpu, PrintWriter output) {
            this.cpu = cpu;
            this.output = output;
    }
    
    /**
     * Start the Channel 1 Input Spool task.
     * @param emptyBuffer A buffer from the empty buffer queue
     * @return whether the task could be started or not
     */
    public boolean startTask(Buffer outputBuffer) {
        if (busy) return false;
        buffer = outputBuffer;
        timer = 0;
        busy = true;
        return true;
    }
    
    /**
     * Increase the timer if a task is in progress.
     */
    public void cycleComplete() {
        timer++;
        if (busy && timer == Channel2.TIME_REQUIRED) {
            //Complete simulation
            output.println(buffer);
            buffer.setEmpty();
            cpu.IOI += Channel2.IOI_VALUE;
            busy = false;
        }
            
    }
    /**
     * Get the buffer Channel 1 worked on
     * @return The buffer Channel 1 worked on 
     */
    public Buffer getBuffer() {
        return buffer;
    }
    
    /**
     * Get the value of the timer.
     * @return the value of the timer
     */
    public int getTimer() {
        return timer;
    }
    
    /**
     * 
     * @return whether busy 
     */
    public boolean isBusy() {
        return busy;
    }
}
